/*
 * MIT License
 *
 * Copyright (c) 2023 北京凯特伟业科技有限公司
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.je.common.base.office.service;

import com.je.common.base.DynaBean;
import com.je.common.base.cache.office.SealCacheManager;
import com.je.common.base.mapper.query.NativeQuery;
import com.je.common.base.service.MetaResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("jeSealService")
public class JeSealServiceImpl implements JeSealService {
    @Autowired
    private SealCacheManager sealCacheManager;
    @Autowired
    private MetaResourceService metaResourceService;
    @Override
    public DynaBean getSealInfo(String key, String type, String refresh) {
        DynaBean seal= sealCacheManager.getCacheValue(type+"_"+key);
        if("1".equals(refresh)){
            seal=null;
        }
        if(seal==null){
            NativeQuery nativeQuery= NativeQuery.build();
//            String whereSql=" AND GZXX_CODE='"+key+"' AND GZXX_GZFL_CODE='TY'";
//            String whereSql=" AND SEAL_CODE='"+key+"'";
            if("GS".equals(type)){
                nativeQuery.eq("GSID",key).eq("SEAL_GZFL_CODE","GS");
//                whereSql=" AND GSID='"+key+"' AND SEAL_GZFL_CODE='GS'";
            }else if("DEPT".equals(type)){
                nativeQuery.like("DEPTID",key).eq("SEAL_GZFL_CODE","DEPT");
//                whereSql=" AND DEPTID LIKE '%"+key+"%' AND SEAL_GZFL_CODE='DEPT'";
            }else if("HT".equals(type)){
                nativeQuery.eq("BASE_CODE",key).eq("SEAL_GZFL_CODE","HT");
//                whereSql=" AND BASE_CODE='"+key+"' AND SEAL_GZFL_CODE='HT'";
            }else if("USER".equals(type)){
                nativeQuery.eq("USERID",key).eq("SEAL_GZFL_CODE","USER");
//                whereSql=" AND USERID='"+key+"' AND SEAL_GZFL_CODE='USER'";
            }else if("USERNAME".equals(type)){
                nativeQuery.eq("USERNAME",key).eq("SEAL_GZFL_CODE","USER");
//                whereSql=" AND USERNAME='"+key+"' AND SEAL_GZFL_CODE='USER'";
            }else{
                nativeQuery.like("SEAL_CODE",key);
            }
            seal=metaResourceService.selectOneByNativeQuery("JE_CORE_SEAL",nativeQuery);
            if(seal!=null){
                sealCacheManager.putCache(type+"_"+key,seal);
            }
        }
        return seal;
    }
}
